package org.jeecg.modules.deal.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.deal.entity.DesReportTable;
import org.jeecg.modules.deal.entity.StatisticsTable;
import org.jeecg.modules.deal.service.DesReportTableService;
import org.jeecg.modules.meta.service.ISysTableinfoService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @Description: 订阅查询
 * @Author: jeecg-boot
 * @Date: 2019-07-08
 * @Version: V1.0
 */
@Slf4j
@Api("订阅查询")
@RestController
@RequestMapping("/deal/desReportTable")
public class DesReportTableController {
    @Autowired
    private DesReportTableService desReportTableService;

    @Autowired
    ISysTableinfoService iSysTableinfoService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订阅查询-分页列表查询")
    @ApiOperation(value = "订阅查询-分页列表查询", notes = "订阅查询-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<DesReportTable>> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       @RequestParam(name = "fdTableName", required = false) String fdTableName,
                                                       @RequestParam(name = "fdSchema", required = false) String fdSchema,
                                                       @RequestParam(name = "fdProNum", required = false) String fdProNum,
                                                       HttpServletRequest req) {
        Result<IPage<DesReportTable>> result = new Result<>();
        Page<DesReportTable> page = new Page<DesReportTable>(pageNo, pageSize);
        IPage<DesReportTable> pageList = desReportTableService.queryStrategyHasSSc(page, fdTableName, fdSchema,fdProNum);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog(value = "策略查询-分页列表查询")
    @ApiOperation(value = "策略查询-分页列表查询", notes = "订阅查询-分页列表查询")
    @GetMapping(value = "/Strategylist")
    public Result<IPage<DesReportTable>> queryStrategyPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                               @RequestParam(name = "fdTableName", required = false) String fdTableName,
                                                               @RequestParam(name = "fdSchema", required = false) String fdSchema,
                                                               HttpServletRequest req) {
        Result<IPage<DesReportTable>> result = new Result<>();
        Page<DesReportTable> page = new Page<DesReportTable>(pageNo, pageSize);
        IPage<DesReportTable> pageList = desReportTableService.queryStrategyTable(page, fdTableName,  fdSchema);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订阅范围查询-分页列表查询")
    @ApiOperation(value = "订阅范围查询-分页列表查询", notes = "订阅范围查询-分页列表查询")
    @GetMapping(value = "/queryRange")
    public Result<IPage<DesReportTable>> queryRangeList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(name = "fdTableName", required = false) String fdTableName,
                                                        @RequestParam(name = "fdTableinfoType", required = false) String fdTableinfoType,
                                                        @RequestParam(name = "runDate_begin", required = false) String runDate_begin,
                                                        @RequestParam(name = "runDate_end", required = false) String runDate_end,
                                                        HttpServletRequest req) {
        Result<IPage<DesReportTable>> result = new Result<>();
        Page<DesReportTable> page = new Page<DesReportTable>(pageNo, pageSize);
        IPage<DesReportTable> pageList = desReportTableService.queryRunDetail(page, fdTableName, runDate_begin, runDate_end,fdTableinfoType);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "统计报表查询-分页列表查询")
    @ApiOperation(value = "统计报表查询-分页列表查询", notes = "订阅范围查询-分页列表查询")
    @GetMapping(value = "/queryStatisticsTable")
    public Result<IPage<StatisticsTable>> queryStatisticsTable(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        Result<IPage<StatisticsTable>> result = new Result<>();
        Page<StatisticsTable> page = new Page<StatisticsTable>(pageNo, pageSize);
        IPage<StatisticsTable> pageList = desReportTableService.queryStatisticsTable(page);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订阅查询-通过id查询")
    @ApiOperation(value = "订阅查询-通过id查询", notes = "订阅查询-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<DesReportTable> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<DesReportTable> result = new Result<DesReportTable>();
        DesReportTable DesReportTable = desReportTableService.getById(id);
        if (DesReportTable == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(DesReportTable);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        // Step.1 组装查询条件
        QueryWrapper<DesReportTable> queryWrapper = null;
        try {
            String paramsStr = request.getParameter("paramsStr");
            if (oConvertUtils.isNotEmpty(paramsStr)) {
                String deString = URLDecoder.decode(paramsStr, "UTF-8");
                DesReportTable DesReportTable = JSON.parseObject(deString, DesReportTable.class);
                queryWrapper = QueryGenerator.initQueryWrapper(DesReportTable, request.getParameterMap());
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<DesReportTable> pageList = desReportTableService.list(queryWrapper);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "订阅查询列表");
        mv.addObject(NormalExcelConstants.CLASS, DesReportTable.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("订阅查询列表数据", "导出人:Jeecg", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }


}
